﻿using SimplePersonManager.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimplePersonManager
{
    public class InMemoryRepository : IRepository
    {
        List<Person> _people = new List<Person>();

        public InMemoryRepository()
        {
            _people.Add(new Person()
            {
                Born = new DateTime(1980, 2, 7, 11, 0, 0),
                City = "bryne",
                Country = "norway",
                Name = "name1",
                Post_Code = "4341"
            });
            _people.Add(new Person()
            {
                Born = new DateTime(1976, 3, 2, 1, 10, 00),
                City = "milan",
                Country = "italy",
                Name = "name2",
                Post_Code = "20030"
            });
        }

        public void StorePerson(Person person)
        {
            var personAlreadyStored = RetrievePerson(person.Name);

            if (personAlreadyStored != null)
                throw new ArgumentException(string.Format("Person with name '{0}' is already stored.", person.Name));

            _people.Add(person);
        }

        public Person RetrievePerson(string name)
        {
            var person = _people.SingleOrDefault(p => p.Name == name);
            return person;
        }

        public IEnumerable<Person> RetrieveAll()
        {
            return _people;
        }
    }
}
